package com.vdg.fps.app;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;

import com.vdg.fps.config.ErrorCode;
import com.vdg.fps.config.FPSConfig;
import com.vdg.fps.data.FaceBookAccount;
import com.vdg.fps.listener.OnQueryListAccountListener;
import com.vdg.fps.listener.OnRequestListnener;

public class FPSManagerment {
	private static FPSManagerment sInstance;
	private ArrayList<FaceBookAccount> mListFaceBookAccount;
	private OnQueryListAccountListener mOnQueryListAccountListener;
	private Handler mHandler;
	private OnRequestListnener mOnRequestListnener;

	public static class TimeOutRunable implements Runnable {
		private OnRequestListnener mOnRequestListnener;

		public TimeOutRunable(OnRequestListnener listener) {
			mOnRequestListnener = listener;
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			mOnRequestListnener.onFailed(ErrorCode.REQUEST_TIME_OUT_ERROR);
		}
	}

	public void checkConstainAndUpdate(FaceBookAccount fb) {
		for (FaceBookAccount acc : mListFaceBookAccount) {
			if (acc.getID().equals(fb.getID())) {
				mListFaceBookAccount.set(mListFaceBookAccount.indexOf(acc), fb);
				return;
			}
		}
		mListFaceBookAccount.add(fb);
	}

	public static FPSManagerment getInstace() {
		if (sInstance == null) {
			sInstance = new FPSManagerment();
		}
		return sInstance;
	}

	public FPSManagerment() {
		// TODO Auto-generated constructor stub
		mHandler = new Handler();
		mListFaceBookAccount = new ArrayList<FaceBookAccount>();
	}

	public void setUpAccount(final Activity activity,
			final FaceBookAccount account, final OnRequestListnener listener) {
		account.getAcceToken(new OnRequestListnener() {

			@Override
			public void onSuccess(String response) {
				// TODO Auto-generated method stub
				account.getAccountInfors(new OnRequestListnener() {

					@Override
					public void onSuccess(String response) {
						// TODO Auto-generated method stub
						account.GetFBDTSG(new OnRequestListnener() {

							@Override
							public void onSuccess(String response) {
								// TODO Auto-generated method stub
								account.setLanguage(response, "en_US");
								checkConstainAndUpdate(account);
								mOnQueryListAccountListener
										.onResponse(mListFaceBookAccount);
								account.save(activity);
								activity.setResult(activity.RESULT_OK);
								activity.finish();
								listener.onSuccess(response);
							}

							@Override
							public void onFailed(int errorCode) {
								// TODO Auto-generated method stub
								listener.onFailed(errorCode);
							}
						});
					}

					@Override
					public void onFailed(int errorCode) {
						// TODO Auto-generated method stub
						listener.onFailed(errorCode);
					}
				});
			}

			@Override
			public void onFailed(int errorCode) {
				// TODO Auto-generated method stub
				listener.onFailed(errorCode);
			}
		});
	}

	public synchronized void queryListAccount(
			OnQueryListAccountListener listener, Context context) {
		mOnQueryListAccountListener = listener;
		if (mListFaceBookAccount == null || mListFaceBookAccount.size() == 0) {
			mListFaceBookAccount = FaceBookAccount.getListAccountFile(context);
		}
		mOnQueryListAccountListener.onResponse(mListFaceBookAccount);
	}

	public synchronized FaceBookAccount getFacebookAccount(int index) {
		try {
			return mListFaceBookAccount.get(index);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public synchronized void getListAccountsFromFile(Context context) {
		this.mListFaceBookAccount = FaceBookAccount.getListAccountFile(context);
	}

	public synchronized void fetchGroupsList(FaceBookAccount account,
			OnRequestListnener listener) {
		TimeOutRunable timeout = new TimeOutRunable(listener);
		mHandler.postDelayed(timeout, FPSConfig.TIME_OUT_TIME);
		account.getGroupsInfor(listener, mHandler, timeout);
	}

	public synchronized void fetchFriendsList(FaceBookAccount account,
			OnRequestListnener listener) {
		TimeOutRunable timeout = new TimeOutRunable(listener);
		mHandler.postDelayed(timeout, FPSConfig.TIME_OUT_TIME);
		account.getFriendsInfor(listener, mHandler, timeout);
	}

	public synchronized void getMembersInGroup(FaceBookAccount account,
			String groupID, OnRequestListnener listener) {
		TimeOutRunable timeout = new TimeOutRunable(listener);
		mHandler.postDelayed(timeout, FPSConfig.TIME_OUT_TIME);
		account.getMembersInGroup(groupID, listener, mHandler, timeout);
	}

	public synchronized void getLikes(FaceBookAccount account,
			OnRequestListnener listener) {
		TimeOutRunable timeout = new TimeOutRunable(listener);
		mHandler.postDelayed(timeout, FPSConfig.TIME_OUT_TIME);
		account.getLikes(listener, mHandler, timeout);
	}

	public synchronized void getMyPages(FaceBookAccount account,
			OnRequestListnener listener) {
		TimeOutRunable timeout = new TimeOutRunable(listener);
		mHandler.postDelayed(timeout, FPSConfig.TIME_OUT_TIME);
		account.getMyPages(listener, mHandler, timeout);
	}
}
